package com.poobo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.poobo.aop.SystemCtrLo4jAop;
import com.poobo.condiction.EquipmentConfigListCondiction;
import com.poobo.entity.TbEquipmentConfiglist;
import com.poobo.service.IConfigListService;
import com.poobo.service.IEquipmentConfigListService;
import com.poobo.service.IEquipmentService;
import com.poobo.util.pager.Page;

@Controller
@RequestMapping("equipmentConfig")
public class EquipmentlConfigListCtr {
	@Autowired
	private IEquipmentConfigListService equipmentConfigListService;
	@Autowired
	private IEquipmentService equipmentService;
	@Autowired
	private IConfigListService configListService;
	
	/**
	 * 设备配置列表
	 * @param request
	 * @param page
	 * @param equipmentConfiglist
	 * @return
	 */
	@RequestMapping("toEquipmentConfigListPage")
	public String toEquipmentConfigListPage( HttpServletRequest request, Page page,
			EquipmentConfigListCondiction equipmentConfiglist ){
		if( equipmentConfiglist.getEquipment() != null && equipmentConfiglist.getEquipment().getNumber() != null ){
			request.setAttribute("number",equipmentConfiglist.getEquipment().getNumber());
		}
		page = equipmentConfigListService.queryEquipmentConfigPage(equipmentConfiglist, page);
		request.setAttribute("pageNo", page.getPageNo());
		request.setAttribute("pageNum", page.getPageNum());
		request.setAttribute("pageSize", page.getPageSize());
		request.setAttribute("equipmentConfigs", page.getPageList());
		List<Map<String, Object>> list = (List<Map<String, Object>>) page.getPageList();
		Map<String, List<Map<String, Object>>> map =  
				new HashMap<String, List<Map<String, Object>>>();
		Map<String, Object> temp = null;
		String temp_key = null;
		for( int i=0; i < list.size(); i++ ){
			temp = list.get(i);
			temp_key = temp.get("number")+"";
			if(map.containsKey(temp_key)){
				map.get(temp_key).add(temp);
			}
			else{
				List<Map<String, Object>> r_list = new ArrayList<Map<String, Object>>();
				r_list.add(temp);
				map.put(temp_key, r_list);
			}
		}
		request.setAttribute("map", map);
		return "equipmentConfig/equipment_config";
	}
	/**
	 * 跳转到编辑页面
	 */
	@RequestMapping("toeditEquipmentConfigList")
	public String toeditEquipmentConfigList( HttpServletRequest request,TbEquipmentConfiglist equipmentConfiglist ){
		Integer id = Integer.parseInt(request.getParameter("equipmentConfigId"));
		equipmentConfiglist = equipmentConfigListService.queryEquipmentConfigById(id);
		request.setAttribute("equipmentConfiglist", equipmentConfiglist);
		request.setAttribute("equipment", equipmentService.queryEquipmentById(equipmentConfiglist.getEid()));
		request.setAttribute("configList", configListService.queryConfigListById(equipmentConfiglist.getConfigListId()));
		request.setAttribute("equipments", equipmentService.querySome());
		request.setAttribute("configLists", configListService.queryByIsEnable());//已启用的配置列表
		return "equipmentConfig/edit_equipment_configList";
	}
	/**
	 * 跳转到新增页面
	 */
	@RequestMapping("tosaveEquipmentConfigList")
	public String tosaveEquipmentConfigList( HttpServletRequest request ){
		request.setAttribute("equipments", equipmentService.querySome());
		request.setAttribute("configLists", configListService.queryByIsEnable());//已启用的配置列表
		return "equipmentConfig/save_equipment_configList";
	}
	/**
	 * 保存
	 * @param equipmentConfiglist
	 * @return
	 */
	@RequestMapping("saveEquipmentConfigList")
	@SystemCtrLo4jAop(description="新增设备配置列表")
	public String saveEquipmentConfigList( TbEquipmentConfiglist equipmentConfiglist ){
		equipmentConfigListService.saveEquipmentConfig(equipmentConfiglist);
		return "redirect:/equipmentConfig/toEquipmentConfigListPage.html";
	}
	/**
	 * 编辑
	 * @param equipmentConfiglist
	 * @return
	 */
	@RequestMapping("editEquipmentConfigList")
	@SystemCtrLo4jAop(description="新增设备配置列表")
	public String editEquipmentConfigList( TbEquipmentConfiglist equipmentConfiglist ){
		equipmentConfigListService.saveEquipmentConfig(equipmentConfiglist);
		return "redirect:/equipmentConfig/toEquipmentConfigListPage.html";
	}
	/**
	 * 根据ID删除
	 * @param request
	 * @return
	 */
	@RequestMapping("deleteById")
	@SystemCtrLo4jAop(description="删除单个设备配置列表")
	public String deleteById( HttpServletRequest request ){
		Integer id = Integer.parseInt(request.getParameter("equipmentConfigId"));
		equipmentConfigListService.deleteEquipmentConfigById(id);
		return "redirect:/equipmentConfig/toEquipmentConfigListPage.html";
	}
	@RequestMapping("deleteByIds")
	@SystemCtrLo4jAop(description="批量删除设备配置列表")
	public String deleteByIds( HttpServletRequest request ){
		String ids = request.getParameter("ids");
		String[] idss = ids.split(",");
		Integer id = null;
		for (String idStr : idss) {
			id = Integer.parseInt(idStr);
			equipmentConfigListService.deleteEquipmentConfigById(id);
		}
		return "redirect:/equipmentConfig/toEquipmentConfigListPage.html";
	}
	
	@RequestMapping("findEquipmentConfingList")
	@ResponseBody
	public String findEQCL(){
		List<Map<String, String>> list = equipmentConfigListService.findEQCL();
		Map<String, String> map = new HashMap<String, String>(); 
		StringBuffer sbfBuffer = new StringBuffer();
		for( int i=0; i<list.size(); i++ ){
			map = list.get(i);
			String number = map.get("number");
			String txtURL = map.get("txtURL");
			if( i==list.size()-1 ){
				sbfBuffer.append(number + "," + txtURL );
			}else {				
				sbfBuffer.append(number + "," + txtURL + "|");
			}
		}
		return sbfBuffer.toString();
	}
}
